home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte22 / ex8.c < prev    next >
C/C++ Source or Header  |  1995-06-04  |  4KB  |  79 lines

  1. #include <genstub.c>
  2. #include <winnls.h>
  3.  
  4. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  5. {
  6.    static BOOL fEnglish = TRUE;
  7.    switch (uMsg)
  8.    {
  9.          case WM_COMMAND:
  10.                switch ( LOWORD( wParam ) )
  11.                {
  12.                      case IDM_TEST:
  13.                      {
  14.                            int    iChars;
  15.                            TCHAR  szNoun[16];          // noun comes 1st in Spanish
  16.                            TCHAR  szAdjective[16];     // adj comes 1st in English
  17.                            TCHAR  szMessageFormat[10]; // smart format string
  18.                            LPTSTR pArgs[16];           // list of args
  19.                            PVOID  pBuffer = 0;         // system allocates buffer
  20.                            HDC    hDC = GetDC( hWnd );
  21.                            static int row = 0;
  22.  
  23.                            pArgs[0] = szAdjective;     // argument 1 = adjective
  24.                            pArgs[1] = szNoun;          // argument 2 = noun
  25.                            pArgs[2] = 0;
  26.  
  27.                            fEnglish = !fEnglish;
  28.                            if (fEnglish) { // If English.
  29.                               lstrcpy( szMessageFormat, "%1 %2" );
  30.                               lstrcpy( szNoun, "States" );
  31.                               lstrcpy( szAdjective, "United" );
  32.                            }
  33.                            else {  // If Spanish.
  34.                               lstrcpy(szMessageFormat, "%2 %1" );
  35.                               lstrcpy( szNoun, "Estados" );
  36.                               lstrcpy( szAdjective, "Unidos" );
  37.                            }
  38.  
  39.                            // Do smart formatting
  40.                            iChars = FormatMessage( FORMAT_MESSAGE_FROM_STRING |
  41.                                                    FORMAT_MESSAGE_ALLOCATE_BUFFER |
  42.                                                    FORMAT_MESSAGE_ARGUMENT_ARRAY ,
  43.                                                    szMessageFormat, 0, 0, &pBuffer,
  44.                                                    63, pArgs );
  45.  
  46.                            // Do output to screen.
  47.                            if (GetLastError()==ERROR_SUCCESS) {
  48.                               TCHAR szOutputBuffer[128];  // output buffer
  49.                               lstrcpy( szOutputBuffer,
  50.                                        "Comparison of Format Message and wvsprintf" );
  51.                               TextOut( hDC, 0, row, szOutputBuffer,
  52.                                        lstrlen( szOutputBuffer ) );
  53.                               TextOut( hDC, 0, row + 20, szOutputBuffer, wsprintf(
  54.                                        szOutputBuffer, "Formatted message: %s",
  55.                                        pBuffer ) );
  56.                               TextOut( hDC, 0, row + 40, szOutputBuffer, wvsprintf(
  57.                                        szOutputBuffer, "Order of the args: %s %s",
  58.                                        pArgs ) );
  59.                               row += 60;
  60.                               // Free the memory system allocated.
  61.                               if (pBuffer)
  62.                                  LocalFree( pBuffer );
  63.                            }
  64.                            ReleaseDC( hWnd, hDC );
  65.                      }
  66.                      break;
  67.                      case IDM_EXIT:
  68.                            DestroyWindow( hWnd );
  69.                      break;
  70.                }
  71.                break;
  72.          case WM_DESTROY:
  73.                PostQuitMessage( 0 );
  74.                break;
  75.          default:
  76.                return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  77.    }
  78.    return( NULL );
  79. }